﻿// <copyright file="MainMenu.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class MainMenu : Menu
{
    public UnityEngine.GameObject LabelPrefab;
    public UnityEngine.GameObject BackgroundPrefab;

    private IViewService viewService;
    private UnityEngine.GUIText wallOfTextLabel;
    private UnityEngine.GUITexture wallOfTextBackground;

    private static string wallOfText = "« C’est comme qui dirait le jeu que les Grecs ont appelé ostomachion. Ce sont des pièces en os, quatorze en tout, qui représentent des figures géométriques. Il y en a d’équilatérales, d’autres avec des côtés de longueur variée, ou encore symétriques, avec des angles droits ou obliques. On les appelle isocèles, isopleures, orthogones scalènes. Grâce aux agencements divers de ces assemblages, on représente mille sortes de formes un éléphant monstrueux ou un sanglier brutal, une oie qui vole et un mirmillon sous les armes, un chasseur à l’affût et un chien qui aboie, et encore une tour, un canthare, une quantité innombrable de figures qui varient selon la plus ou moins habileté de chacun. Les compositions des gens adroits sont étonnantes, les assemblages des maladroits ridicules. »\nAusone (IVe s. apr. J.-C.), Lettre à Paul, 18 (trad. M. Lemoine, revue par V. Dasen)";
        
    public override void Initialize(IGuiController guiController)
    {
        base.Initialize(guiController);

        this.viewService = Services.GetService<IViewService>();

        this.InitializeMenu();
    }
    
    private void InitializeMenu()
    {
        float centerY = UnityEngine.Screen.height / 2;

        float x = 0.5f;

        // Wall of Text.
        UnityEngine.GameObject labelObject = UnityEngine.GameObject.Instantiate(this.LabelPrefab) as UnityEngine.GameObject;
        this.wallOfTextLabel = labelObject.GetComponent<UnityEngine.GUIText>();

        // Place the button.
        UnityEngine.Transform labelTransform = labelObject.transform;
        labelTransform.parent = this.transform;
        this.wallOfTextLabel.text = MainMenu.wallOfText;
        this.wallOfTextLabel.alignment = UnityEngine.TextAlignment.Left;
        this.wallOfTextLabel.anchor = UnityEngine.TextAnchor.UpperLeft;
        this.wallOfTextLabel.fontSize = UnityEngine.Mathf.RoundToInt(this.wallOfTextLabel.fontSize * GuiHelper.GetScreenSizeRatio());

        UnityEngine.Rect rect = GuiHelper.FormatGuiTextArea(this.wallOfTextLabel, UnityEngine.Screen.width * 0.9f);
        ////rect.height = 64;
        ////rect.x = -rect.width / 2;
        ////rect.y = -rect.height / 2;
        ////currentButton.GuiTexture.pixelInset = rect;
        labelTransform.localPosition = new UnityEngine.Vector3(0.05f, 0.95f, 0);

        this.wallOfTextLabel.pixelOffset = new UnityEngine.Vector2(0, 0);

        // Background.
        UnityEngine.GameObject backgroundObject = UnityEngine.GameObject.Instantiate(this.BackgroundPrefab) as UnityEngine.GameObject;
        this.wallOfTextBackground = backgroundObject.GetComponent<UnityEngine.GUITexture>();

        rect.height *= 1.1f;
        rect.width = UnityEngine.Screen.width;
        rect.x = -rect.width / 2;
        rect.y = -rect.height;

        this.wallOfTextBackground.pixelInset = rect;

        UnityEngine.Transform backgroundTransform = backgroundObject.transform;
        backgroundTransform.localPosition = new UnityEngine.Vector3(x, 0.98f, -1);
        
        // We need 3 buttons.
        // First, the loculus one.
        UnityEngine.GameObject buttonObject = UnityEngine.GameObject.Instantiate(this.ButtonPrefab) as UnityEngine.GameObject;
        Button currentButton = buttonObject.GetComponent<Button>();
        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonLoculus_Clicked;
        
        // Place the button.
        UnityEngine.Transform buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;
        currentButton.GuiText.text = "Commencer";

        rect = GuiHelper.FormatGuiTextArea(currentButton.GuiText, 300);
        rect.height = 50 * GuiHelper.GetScreenSizeRatio();
        rect.width = UnityEngine.Mathf.Max(200 * GuiHelper.GetScreenSizeRatio(), rect.width + (10 * GuiHelper.GetScreenSizeRatio()));
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;

        buttonTransform.localPosition = new UnityEngine.Vector3(x, backgroundTransform.localPosition.y - (this.wallOfTextBackground.pixelInset.height / UnityEngine.Screen.height) - 0.1f, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);
        
        ////// Then, the tangram one.
        ////buttonObject = UnityEngine.GameObject.Instantiate(this.ButtonPrefab) as UnityEngine.GameObject;
        ////currentButton = buttonObject.GetComponent<Button>();

        ////this.buttons.Add(currentButton);
        ////currentButton.IsVisible = this.IsVisible;
        ////currentButton.Clicked += this.ButtonTangram_Clicked;

        ////// Place the button.
        ////buttonTransform = currentButton.transform;
        ////buttonTransform.parent = this.transform;

        ////currentButton.GuiText.text = "Tangram";

        ////rect = GuiHelper.FormatGuiTextArea(currentButton.GuiText, 300);
        ////rect.height = 50;
        ////rect.width = UnityEngine.Mathf.Max(200, rect.width + 10);
        ////rect.x = -rect.width / 2;
        ////rect.y = -rect.height / 2;
        ////currentButton.GuiTexture.pixelInset = rect;
        ////buttonTransform.localPosition = new UnityEngine.Vector3(x, (centerY - 60) / UnityEngine.Screen.height, 0);

        ////currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);

        // Finally, the quit application.
        buttonObject = UnityEngine.GameObject.Instantiate(this.ExitPrefab) as UnityEngine.GameObject;
        currentButton = buttonObject.GetComponent<Button>();

        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonQuit_Clicked;

        // Place the button.
        buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;

        rect.height = 64 * GuiHelper.GetScreenSizeRatio();
        rect.width = 64 * GuiHelper.GetScreenSizeRatio();
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;
        buttonTransform.localPosition = new UnityEngine.Vector3(x, 0.1f, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);
    }

    public override void Show(params object[] parameters)
    {
        base.Show(parameters);

        // Enable wall of text and background.
        this.wallOfTextLabel.gameObject.SetActive(true);
        this.wallOfTextBackground.gameObject.SetActive(true);
    }

    public override void Hide()
    {
        base.Hide();
        
        this.wallOfTextLabel.gameObject.SetActive(false);
        this.wallOfTextBackground.gameObject.SetActive(false);
    }

    private void ButtonLoculus_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.ShowMenu(typeof(PuzzleChoiceMenu), "Loculus");
    }

    private void ButtonTangram_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.ShowMenu(typeof(PuzzleChoiceMenu), "Tangram");
    }

    private void ButtonQuit_Clicked(object sender, System.EventArgs args)
    {
        UnityEngine.Application.Quit();
    }
}
